package com.qiandw.spark.sql.api

import java.util.Date
import javax.servlet.http.HttpServletRequest

import com.qiandw.spark.sql.DemoSrv
import com.qiandw.spark.{SparkContextFactory, SparkSqlSrv}
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.{RequestMapping, RestController}

@RestController
class HomeApi {

  @Autowired
  var scf: SparkContextFactory = _

  @Autowired
  var srv: SparkSqlSrv = _

  @Autowired
  var demoSrv: DemoSrv = _

  /**
    * 注册用户
    */
  @RequestMapping(value = Array("test1.api"))
  def test1(req: HttpServletRequest): Any = {

    val s = (new Date).getTime
    scf.getSparkSession.sql("select qdw_id from member").rdd.collect().toList.foreach(x=>println(x))
    (new Date).getTime - s

  }

  /**
    * 注册用户
    */
  @RequestMapping(value = Array("test2.api"))
  def test2(req: HttpServletRequest): Any = {

    val s = (new Date).getTime
    scf.getSparkSession.sql("select name from t_member_cached").rdd.collect().toList.foreach(x=>println(x))
    (new Date).getTime - s

  }

  /**
    * 注册用户
    */
  @RequestMapping(value = Array("test3.api"))
  def test3(req: HttpServletRequest): Any = {

//    val ss = scf.getSparkSession

    val s = (new Date).getTime

//    ss.sqlContext.uncacheTable("t_member_cached")
//    ss.sqlContext.cacheTable("t_member_cached")

    (new Date).getTime - s

  }

  @RequestMapping(value = Array("test4.api"))
  def test4(req: HttpServletRequest): Any = {

    demoSrv.demo3()

  }

}
